﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PhotoList.Web.Admin
{
    public partial class ucThumb : System.Web.UI.UserControl
    {
        #region Attributes

        protected string[] PhotoType = ConfigurationManager.AppSettings["PicType"].Split(',');
        protected int ThumbnailWidth = Convert.ToInt16(ConfigurationManager.AppSettings["ThumbnailWidth"]);
        protected int ThumbnailHeight = Convert.ToInt16(ConfigurationManager.AppSettings["ThumbnailHeight"]);
        protected string ThumbnailMode = ConfigurationManager.AppSettings["ThumbnailMode"];
        protected bool IsSub = false;
        bool IsOutPic = false;
        protected string PicturePath = "";
        protected string ThumbnailPath = "";

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (CheckQuery())
            {
                case 0:
                    PhotoZoon.Dal.Thumbnail.Create(Server.MapPath(PicturePath), Server.MapPath(ThumbnailPath), ThumbnailWidth, ThumbnailHeight, ThumbnailMode, PhotoType, false);
                    break;
                case 1:
                    if (!(string.IsNullOrEmpty(ConfigurationManager.AppSettings["IsOutPic"])) && ConfigurationManager.AppSettings["IsOutPic"] == "1") IsOutPic = true;
                    PhotoZoon.Dal.Thumbnail.Create(PicturePath, ThumbnailPath, ThumbnailWidth, ThumbnailHeight, ThumbnailMode, PhotoType, false, IsSub, IsOutPic);
                    break;
                default:
                    break;
            }
        }

        protected int CheckQuery()
        {
            //判断源和目标是否为空
            if (string.IsNullOrEmpty(Request.QueryString["dir"]))
            {
                return -1;
            }

            string PictureDirectory = ConfigurationManager.AppSettings["HomeDirectory"];
            string ThumbnailDirectory = ConfigurationManager.AppSettings["ThumbnailPath"];
            string Home = new PhotoList.Dal.User(Session["QT_UID"].ToString()).Home;
            string Dir = Request.QueryString["dir"];


            //获取源和目标地址
            PicturePath = PictureDirectory + Home + Dir;
            ThumbnailPath = ThumbnailDirectory + Home + Dir;

            if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"].ToLower() == "file") return 0;

            //是否包含子目录
            if (string.IsNullOrEmpty(Request.QueryString["issub"])) IsSub = false;
            else
            {
                switch (Request.QueryString["issub"].ToString())
                {
                    case "true":
                        IsSub = true;
                        break;
                    case "false":
                        IsSub = false;
                        break;
                    default:
                        IsSub = false;
                        break;
                }
            }

            return 1;
        }
    }
}